package com.open.party.cloud.view.examine

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.sinothk.hussars.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.*
import com.open.party.cloud.view.examine.adapter.ExamineClassTeacherListAdapter
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_app_bg.loadingRecyclerView
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_app_bg.loadingTipView
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineClassTeacherRankListActivity : AppEventBusRecycleViewTitleBaseActivity<String>(), OnSuperListener<CourseTeacherEntity> {

    var viewModel: ExamineViewModel? = null
    var classId: String? = null

    private var adapter: ExamineClassTeacherListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_app_bg

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        classId = intent.getStringExtra("classId")
        setViewTitle("教师评估")

        viewModel = ExamineViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = ExamineClassTeacherListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    override fun loadData(pageVo: PageVo<String>) {
//        val pageReq = PageReq<ExamineClassVO>()
//        pageReq.pageNo = pageVo.pageNum
//        pageReq.pageSize = 10
//        val vo = ExamineClassVO()
//        vo.classId = classId
//        pageReq.data = vo
        viewModel!!.getCourseTeacherList(classId!!)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<List<CourseTeacherEntity>>) {
        if (result.eventType != "getCourseTeacherList") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    if (result.data.isEmpty()) {
                        loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                        loadingRecyclerView.setLoadingMoreEnabled(false)
                    } else {
                        adapter!!.setData(result.data!!)
                        loadingTipView.showContent()
                    }
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onClick(position: Int, itemData: CourseTeacherEntity, flag: String?) {
        XUtils.intent().openActivity(this, ExamineClassTeacherRankActivity::class.java)
            .putSerializableExtra("courseTeacher",itemData)
            .start()
    }
}